<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" media="screen" href="../../css/bootstrap.min.css">
    <link rel="stylesheet" type="text/css" media="screen" href="../../layui/css/layui.css">
</head>
<body>
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
    <form class="form-horizontal" onsubmit="return false" id="form">
        <fieldset>

            <div class='form-group'>
                <label class='col-md-2 control-label'>图片上传</label>
                <div class='col-md-10'>
                    <button type="button" class="layui-btn" id="test1">
                        <i class="layui-icon">&#xe67c;</i>上传图片
                    </button>
                </div>
            </div>
            <div class='form-group'>
                <label class='col-md-2 control-label'>图片</label>
                <div class='col-md-10'>
                    <img id='imgUrl' height="120" width="120"/>
                </div>
            </div>


            <input class='form-control' placeholder='path' type='hidden' name='path' id='path'>
            <input class='form-control' placeholder='url' type='hidden' name='url' id='url'>


            <div class='form-group'>
                <label class='col-md-2 control-label'>排序：</label>
                <div class='col-md-10'>
                    <input class='form-control' placeholder='order' type='text' name='order' id='order'
                           data-bv-notempty='true' data-bv-notempty-message='order 不能为空'>
                </div>
            </div>

            <div class='form-group'>
                <label class='col-md-2 control-label'>服务类型：</label>
                <div class='col-md-10'>
                    <select name="serviceType" class="form-control input-sm" id="serviceType">
                        <option value="">请选择</option>
                        <option value="0">外部链接</option>
                        <option value="1">内部链接</option>
                        <option value="2">律师详情</option>
                        <option value="3">资讯</option>
                        <option value="4">免费咨询</option>
                        <option value="5">专业咨询</option>
                        <option value="6">律师函</option>
                        <option value="7">合同审核</option>
                        <option value="8">合同定制</option>

                    </select>
                </div>
            </div>

            <div class='form-group'>
                <label class='col-md-2 control-label'>广告分类：</label>
                <div class='col-md-10'>
                    <select name="advertisingType" class="form-control input-sm" id="advertisingType">
                        <option value="">请选择</option>
                        <option value="0">用户端</option>
                        <option value="1">律师端</option>
                    </select>
                </div>
            </div>

            <div class='form-group'>
                <label class='col-md-2 control-label'>跳转参数（url以外填写数据id）：</label>
                <div class='col-md-10'>
                    <input class='form-control' placeholder='skipUrl' type='text' name='skipUrl' id='skipUrl'
                           data-bv-notempty='false' data-bv-notempty-message='skipUrl 不能为空'>
                </div>
            </div>


            <div class="form-actions">
                <div class="row" align="center">
                    <div class="col-md-12">
                        <button class="btn btn-primary" onclick="location.href='mfflAdvertisingList.html'">返回</button>
                        <button class="btn btn-primary" id="save" type="submit" onclick="add()">
                            <i class="fa fa-save"></i> 保存
                        </button>
                    </div>
                </div>
            </div>

        </fieldset>
    </form>
</div>
<script type="text/javascript" src="../../js/libs/jquery-2.1.1.min.js"></script>
<script type="text/javascript" src="../../js/jq.js"></script>
<script type="text/javascript" src="../../js/plugin/bootstrapvalidator/bootstrapValidator.min.js"></script>
<script type="text/javascript" src="../../js/common.js"></script>
<script type="text/javascript" src="../../layui/layui.js"></script>
<script type="text/javascript">
    layui.use(['layer', 'laydate'], function () {
        var layer = layui.layer;
    });

    layui.use('upload', function () {
        var upload = layui.upload;
        upload.render({
            elem: '#test1' //绑定元素
            , accept: 'file' //允许上传的文件类型
            , url: '/files/scAdvert' //上传接口
            , done: function (res, index, upload) {
                console.log(res);
                layer.msg("上传成功");
                $('#path').val(res.path);
                $('#url').val(res.url);
                $('#imgUrl').attr('src', '/statics' + res.url);
                document.getElementById("save").removeAttribute("disabled");
            }
        });
    });

    function add() {

        var serviceType = $("#serviceType").val();
        if(serviceType.trim() == ""){
            layer.msg("服务类型不能为空")
            return;
        }
        var advertisingType = $("#advertisingType").val();
        if(advertisingType.trim() == ""){
            layer.msg("广告分类不能为空")
            return;
        }

        $('#form').bootstrapValidator();
        var bootstrapValidator = $("#form").data('bootstrapValidator');
        bootstrapValidator.validate();
        if (!bootstrapValidator.isValid()) {
            return;
        }

        var formdata = $("#form").serializeObject();

        console.log(formdata.path);
        if (formdata.path == '' ){
            layer.msg("请上传图片");
            return;
        }
        $.ajax({
            type: 'post',
            url: '/mfflAdvertisings',
            contentType: "application/json; charset=utf-8",
            data: JSON.stringify(formdata),
            success: function (data) {
                layer.msg("添加成功", {shift: -1, time: 1000}, function () {
                    location.href = "mfflAdvertisingList.html";
                });
            }
        });
    }

</script>
</body>
</html>
